<template>
	<el-form size="large" class="login-content-form" ref="formRef" :rules="formRules" :model="formData" @keyup.enter.native="onLogin">
		<el-form-item class="login-animation1" prop="Account">
			<el-input text placeholder="请输入用户名" v-model="formData.Account" clearable autocomplete="off">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-User /></el-icon>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation2" prop="PassWord">
			<el-input :type="formValue.isShowPassword ? 'text' : 'password'" placeholder="请输入密码" v-model="formData.PassWord" autocomplete="off">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-Unlock /></el-icon>
				</template>
				<template #suffix>
					<i
						class="iconfont el-input__icon login-content-password"
						:class="formValue.isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
						@click="formValue.isShowPassword = !formValue.isShowPassword"
					>
					</i>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="login-animation3" prop="Captcha">
			<el-col :span="15">
				<el-input text maxlength="4" placeholder="请输入验证码" v-model="formData.Captcha" clearable autocomplete="off">
					<template #prefix>
						<el-icon class="el-input__icon"><ele-Position /></el-icon>
					</template>
				</el-input>
			</el-col>
			<el-col :span="1"></el-col>
			<el-col :span="8">
				<el-image :src="formValue.Captcha" class="login-content-code" @click="loadCaptcha" />
			</el-col>
		</el-form-item>
		<el-form-item class="login-animation4">
			<el-button type="primary" class="login-content-submit" round v-waves @click="onLogin">
				<span>登 录</span>
			</el-button>
		</el-form-item>
	</el-form>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { Post, PostAsync } from '/@/common/http';
import EMsg from '/@/common/eMsg';

import { useRoute, useRouter } from 'vue-router';
import { initRoutes } from '/@/router/router';
import { Session } from '/@/utils/storage';
import { formatAxis } from '/@/utils/formatTime';
import { MD5Encrypt } from '/@/utils/encryption';

const route = useRoute();
const router = useRouter();

const formValue = ref({
	isShowPassword: false,
	Captcha: '',
});

const formRef = ref();
const formRules = ref({
	Account: [{ required: true, message: '账号不可为空', trigger: 'blur' }],
	PassWord: [{ required: true, message: '密码不可为空', trigger: 'blur' }],
	Captcha: [{ required: true, message: '验证码不可为空', trigger: 'blur' }],
});
const formData = ref({
	Account: '',
	PassWord: '',
	CaptchaKey: '',
	Captcha: '',
});

const loadCaptcha = () => {
	Post('SysLogin/GetCaptcha', null, (res: any) => {
		formData.value.CaptchaKey = res.Data.CaptchaKey;
		formValue.value.Captcha = 'data:image/png;base64,' + res.Data.Captcha;
	});
};

const onLogin = () => {
	formRef.value.validate((valid: any) => {
		if (valid) {
			const data = {
				Account: formData.value.Account,
				PassWord: MD5Encrypt(formData.value.PassWord),
				CaptchaKey: formData.value.CaptchaKey,
				Captcha: formData.value.Captcha,
			};
			PostAsync('SysLogin/Login', data).then(async (res: any) => {
				if (res.Code == 1) {
					Session.set('UserName', res.Data.UserName);
					Session.set('UserId', res.Data.UserId);
					Session.set('UserToken', res.Data.UserToken);
					const isNoPower = await initRoutes();
					signInSuccess(isNoPower);
				} else {
					EMsg.error(res.Msg);
					loadCaptcha();
				}
			});
		}
	});
};

// 登录成功后的跳转
const signInSuccess = (isNoPower: boolean | undefined) => {
	if (isNoPower) {
		EMsg.warning('抱歉，您没有登录权限');
		Session.clear();
	} else {
		let currentTimeInfo = formatAxis(new Date());
		if (route.query?.redirect) {
			router.push({
				path: <string>route.query?.redirect,
				query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
			});
		} else {
			router.push('/');
		}
		// 登录成功提示
		EMsg.success(`${currentTimeInfo}，${Session.get("UserName")}`);
	}
};

onMounted(() => {
	loadCaptcha();
});
</script>

<style scoped lang="scss">
.login-content-form {
	margin-top: 20px;
	@for $i from 1 through 4 {
		.login-animation#{$i} {
			opacity: 0;
			animation-name: error-num;
			animation-duration: 0.5s;
			animation-fill-mode: forwards;
			animation-delay: calc($i/10) + s;
		}
	}
	.login-content-password {
		display: inline-block;
		width: 20px;
		cursor: pointer;
		&:hover {
			color: #909399;
		}
	}
	.login-content-code {
		width: 100%;
		padding: 0;
		font-weight: bold;
		letter-spacing: 5px;
		height: 38px;
		margin-top: 10px;
	}
	.login-content-submit {
		width: 100%;
		letter-spacing: 2px;
		font-weight: 300;
		margin-top: 15px;
	}
}
</style>